package com.hundredapp.oa.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaAction;
import com.hundredapp.oa.domain.OaRole;
import com.hundredapp.oa.repository.ActionManager;
import com.hundredapp.oa.repository.RoleManager;
import com.opensymphony.xwork2.ActionSupport;

@Repository
@Scope("prototype")
public class Role extends ActionSupport {
	
	private List<OaRole> root;
	private int roleid;
	private List<Integer> actions;
	private Object success;
	private String msg;
	private String rolename;
	
	@Autowired
	private RoleManager roleManage;
	@Autowired
	private ActionManager actionManager;
	
	public String allrole(){
		root=roleManage.findRoleList(SelectType.SELECT_ALL);
		return SUCCESS;
	}
	
	public String add() {
		OaRole role = new OaRole();
		role.setRoleName(rolename);
		roleManage.add(role);
		
		success = true;
		msg = "添加成功";
		return SUCCESS;
	}
	
	public String delete() {
		roleManage.delete(roleid);
		
		success = true;
		msg = "删除成功";
		return SUCCESS;
	}
	
	public String updateaction() {
		OaRole role = roleManage.findById(roleid);
		
		List<OaAction> actionList = new ArrayList<OaAction>();
		
		for(int action : actions) {
			OaAction a = actionManager.findById(action);
			actionList.add(a);
		}
		role.setOaActions(actionList);
		roleManage.update(role);
		success = true;
		msg = "修改成功";
		return SUCCESS;
	}
	
	public List<OaRole> getRoot() {
		return root;
	}

	public void setRoleManage(RoleManager roleManage) {
		this.roleManage = roleManage;
	}

	public void setRoleid(int roleid) {
		this.roleid = roleid;
	}

	public void setActions(List<Integer> actions) {
		this.actions = actions;
	}

	public void setActionManager(ActionManager actionManager) {
		this.actionManager = actionManager;
	}

	public Object getSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	


}
